﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wPersonInfo.xaml
    /// </summary>
    public partial class wPersonInfo : Window
    {
        private person _person;

        public person Person { 
            get
            { 
                return _person;
            } 
            set 
            { 
                _person = value; 
                LoadData(); 
            } 
        }

        private ObservableCollection<object> collectionCharact;
        public wPersonInfo()
        {
            InitializeComponent();
        }

        private void checkBoxDriver_Checked(object sender, RoutedEventArgs e)
        {
            if (null == _person) return;
            driver data = getDriverInfo();
            if (null == data)
                data = createDriverInfo();
            data.del = false;
        }

        private void checkBoxDriver_Unchecked(object sender, RoutedEventArgs e)
        {
            if (null == _person) return;
            driver data = getDriverInfo();
            if (null == data) return;
            data.del = true;

        }

        private void checkBoxDispetcher_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxMechanic_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxOperatorFuel_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxOperatorFuel_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxMechanic_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxDispetcher_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void checkBoxOperatorFuel_Checked_1(object sender, RoutedEventArgs e)
        {

        }

        private driver getDriverInfo()
        {
            if (null == _person) return null;
            return _person.driver.FirstOrDefault();
        }

        private driver createDriverInfo()
        {
            if (null == _person) return null;
            driver _driver = new driver { date_medical_spr = new DateTime(1970, 1, 1), del = false, license_number = "", medical_spr_number = "", enddate_medical_spr = new DateTime(1970, 1, 1), @class = "" };
            _person.driver.Add(_driver);
            return _driver;
        }

        private void RebuildObservableCollection()
        {
            collectionCharact = new ObservableCollection<object>();
            driver _driver = getDriverInfo();
            if (null != _driver)
            {
                collectionCharact.Add(_driver);
            }
        }

        private void LoadData()
        {
            //if (_person == null)
            //{
            //    gridMain.DataContext = null;
            //    return;
            //}
            ////RebuildObservableCollection();
            //gridMain.DataContext = _person;
            ////Binding myBinding = new Binding("DataContextProperty");
            ////myBinding.Source = _person;
            ////mainGrid.SetBinding(Grid.DataContextProperty, myBinding);
            ////gridMain.DataContext = _person;
            //driver _driver = getDriverInfo();
            //if (null != _driver && false == _driver.del)
            //{
            //    checkBoxDriver.IsChecked = true;
            ////    CollectionViewSource cvs = (CollectionViewSource)this.Resources["dataCharact"];
            ////    cvs.Source = null;
            ////    cvs.Source = collectionCharact;
            //}
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            gridMain.DataContext = _person;
            CollectionViewSource cvs = (CollectionViewSource)this.Resources["dataCharact"];
            cvs.Source = _person.driver;
        }
    }
}
